<?php

namespace common\mongo;

use Yii;
use yii\mongodb\Collection;
use yii\mongodb\Database;
use yii\mongodb\LogBuilder;
use yii\mongodb\QueryBuilder;

/**
 * Created by PhpStorm.
 * User: henry
 * Date: 2017/6/6
 * Time: 上午10:09
 *
 * 兼容云采购代码
 */


class Connection extends \yii\mongodb\Connection
{

    /**
     * Returns the MongoDB collection with the given name.
     * @param string|array $name collection name. If string considered as the name of the collection
     * inside the default database. If array - first element considered as the name of the database,
     * second - as name of collection inside that database
     * @param bool $refresh whether to reload the collection instance even if it is found in the cache.
     * @return Collection Mongo collection instance.
     */
    public function getCollection($name, $refresh = false)
    {
        if (is_array($name)) {
            list ($dbName, $collectionName) = $name;
            return $this->getDatabase($dbName)->getCollection($collectionName, $refresh);
        } else {
            return $this->getDatabase()->getCollection($name, $refresh);
        }
    }

    /**
     * Selects the database with given name.
     * @param string $name database name.
     * @return Database database instance.
     */
    protected function selectDatabase($name)
    {
        return Yii::createObject([
            'class' => '\common\mongo\DateBase',
            'name' => $name,
            'connection' => $this,
        ]);
    }

}